<?php

namespace app\admin\controller;

use think\Exception;

class Project extends Common
{
    // 检车项目管理
    public function index()
    {
        $data = [];
        // 搜索
        $license_type = input('post.license_type');
        $data['license_type'] = $license_type;
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $model = new \app\admin\model\Project();
        //列表数据
        $list = $model->getList($pageNum, $numPerPage,$data['license_type']);
        $data['list'] = $list;
        //数据总数
        $count = $model->getCount($data['license_type']);
        $data['count'] = $count;
        $data['vehicle'] = db('vehicle')->select();
        return view('', $data);
    }
    // 添加
     public function add()
    {
        $data = [];
        $data['is_company'] = input('get.is_company');
        $data['vehicle'] = db('vehicle')->select();
        $data['project'] = db('inspection')->select();
        $data['company'] = db('company')->field('id, company_name')->where('is_use', '=', '1')->where('is_delete', '=', '0')->select();
        return view('', $data);
    }
    //执行添加
    public function addproject()
    {
        $data = input('post.');
        $navTabId = $data['is_company'] == 0 ? 'projectManage' : 'projectListManage';
        $model = new \app\admin\model\Project();
        try {
            $model->doAdd_project($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '添加成功',
            'navTabId' => $navTabId,
            'callbackType' => 'closeCurrent',
        ]);
    }
    // 执行删除
    public function doDelete()
    {
        $userId = input('get.id');
        $is_company = input('get.is_company');
        $navTabId = $is_company == 0 ? 'projectManage' : 'projectListManage';
        $Project = new \app\admin\model\Project();
        try {
            $Project->doDelete($userId, $is_company);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '删除成功',
            'navTabId' => $navTabId,
        ]);
    }

    // 修改
    public function modify()
    {
        $id = input('get.id');
        $is_company = input('get.is_company');
        if(0 == $is_company){
            $info = db('vehicle_inspection')
                ->where('goods_id',$id)
                ->find();
        }else{
            $info = db('vehicle_inspection_company')
                ->field('id as goods_id,goods_name,license_type,shop_price,sort,company_id')
                ->where('id',$id)
                ->find();
            $info['company_name'] = db('company')->where('id', '=', $info['company_id'])->value('company_name');
        }
        $data['info'] = $info;
        $data['vehicle'] = db('vehicle')->where('id', '=', $info['license_type'])->value('title');
        $this->assign('id',$id);
        $this->assign('is_company',$is_company);
        return view('', $data);
    }

    // 执行修改
    public function domodify()
    {
        $data = input('post.');
        $navTabId = $data['is_company'] == 0 ? 'projectManage' : 'projectListManage';
        try {
            $model = new \app\admin\model\Project();
            $model->doModify_project($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '修改成功',
            'navTabId' => $navTabId,
            'callbackType' => 'closeCurrent',
        ]);
    }

    public function type()
    {
        $data = [];
        // 搜索
        // $title = input('post.title');
        // $data['title'] = $title;
        // //页码
        // $pageNum = 1;
        // if (is_numeric(input('post.pageNum'))) {
        //     $pageNum = input('post.pageNum');
        // }
        // $data['pageNum'] = $pageNum;
        //每页数量
        // $numPerPage = config('num_per_page');
        // $data['numPerPage'] = $numPerPage;
        $project = new \app\admin\model\Project();
        $list = db('inspection')->select();
        // $count = $project->getVehicleCount($title);
        $data['list'] = $list;
        // $data['count'] = $count;
        return view('',$data);
    }

    // 分站检车项目管理
    public function lst()
    {
        $data = [];
        // 搜索
        $company_id = input('post.company_id');
        $data['company_id'] = $company_id;
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $model = new \app\admin\model\Project();
        //列表数据
        $list = $model->getCompanyList($pageNum, $numPerPage,$data['company_id']);
        $data['list'] = $list;
        //数据总数
        $count = $model->getCompanyCount($data['company_id']);
        $data['count'] = $count;
        $data['company'] = db('company')->field('id, company_name')->where('is_delete', 0)->where('is_use', 1)->select();
        return view('', $data);
    }

    /**
     * 启用-禁用
     */
    public function on_off()
    {
        $userId = input('get.id');
        $statusid = input('get.status');
        if ($statusid == 0) {
            $data['id'] = $userId;
            $data['is_use'] = 0;
            $status = "禁用";
        }
        if ($statusid == 1) {
            $data['id'] = $userId;
            $data['is_use'] = 1;
            $status = "启用";
        }
        try {
            $Company = new \app\admin\model\Project();
            $Company->editStatus($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message'    => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message'    => $status . '成功',
            'navTabId'   => 'projectListManage',
        ]);
    }
}